import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';

export default Reflux.createStore({

  listenables: [Actions],

  data: {
    ...PageConst,
    //滚动组件
    infs: null,
    //定位插件
    geolocation: null,
    //搜索服务插件
    placeSearch: null,
    //选择的位置
    position: {longitude: 111.3007521629, latitude: 30.7085782898},
    //我的位置
    myposition: {longitude: 111.3007521629, latitude: 30.7085782898},
    //需要传给外部调用的组建的地点信息
    location: {
      name: '',
      lat: '',
      lng: '',
    },
    //搜索关键字
    keyword: '',
    //地点集合
    pois: [],
    //搜索地点集合
    searchPois: [],
    //是否显示搜索结果
    showSearch: false,
    //当前页数
    pageIndex: 1,
    //本弹窗是否显示
    fullpage_show: false,
  },


  onSetinfs: function (obj) {
    this.data.infs = update(this.data.infs, {$set: obj});
    this.updateComponent();
  },

  //微调选择地点
  onSetMarker: function (obj) {
    console.log(obj);
    this.data.position = update(this.data.position, {longitude: {$set: obj.lng}});
    this.data.position = update(this.data.position, {latitude: {$set: obj.lat}});
    this.updateComponent();
  },

  //微调选择地点
  onSetPoisMarker: function (obj) {
    console.log(obj);
    this.data.position = update(this.data.position, {longitude: {$set: obj.location.lng}});
    this.data.position = update(this.data.position, {latitude: {$set: obj.location.lat}});
    this.updateComponent();
  },

  //初始化方法
  onInit: function () {
    this.data.fullpage_show = update(this.data.fullpage_show, {$set: false});
    this.data.pageIndex = update(this.data.pageIndex, {$set: 1});
    this.data.showSearch = update(this.data.showSearch, {$set: false});
    this.data.pois = update(this.data.pois, {$set: []});
    //this.data.position = update(this.data.position, {$set: {}});
    this.updateComponent();
  },

  onShowPopup: function () {
    this.data.fullpage_show = update(this.data.fullpage_show, {$set: true});
    this.updateComponent();
  },

  onHidePopup: function () {
    this.data.fullpage_show = update(this.data.fullpage_show, {$set: false});
    this.updateComponent();
  },


  onLoadMore: function (resolve, finish) {

    this.data.placeSearch.setPageIndex(this.data.pageIndex);

    this.data.placeSearch.searchNearBy(this.data.keyword, new window.AMap.LngLat(this.data.myposition.longitude, this.data.myposition.latitude), 800, (status, result)=> {
      if (status === 'complete') {
        this.data.pois = update(this.data.pois, {$push: result.poiList.pois});
        this.data.pageIndex = update(this.data.pageIndex, {
          $apply: function (x) {
            return x + 1;
          }
        });
        console.log(result.poiList.pois);
        if (!result.poiList.pois) {
          finish();
        } else {
          resolve();
        }
      } else {
        finish();
      }
      this.updateComponent();
    });
  },

  onSearchNearBy: function () {
    this.data.placeSearch.setPageIndex(this.data.pageIndex);
    this.data.placeSearch.clear();
    this.data.placeSearch.searchNearBy(this.data.keyword, new window.AMap.LngLat(this.data.myposition.longitude, this.data.myposition.latitude), 800, (status, result)=> {
      if (status === 'complete') {
        this.data.pois = update(this.data.pois, {$push: result.poiList.pois});
        this.data.pageIndex = update(this.data.pageIndex, {
          $apply: function (x) {
            return x + 1;
          }
        });
      }
      this.updateComponent();
    });
  },

  onSeachCancel: function () {
    this.data.showSearch = update(this.data.showSearch, {$set: false});
    this.data.keyword = update(this.data.keyword, {$set: ''});
    this.updateComponent();
  },

  onSeachSubmit: function () {
    this.data.showSearch = update(this.data.showSearch, {$set: true});
    this.updateComponent();
  },

  onSearchChange: function (text) {
    this.data.keyword = update(this.data.keyword, {$set: text});
    if (this.data.keyword.length > 3)
      this.data.showSearch = update(this.data.showSearch, {$set: true});
    this.updateComponent();
  },

  onClearPois: function () {
    this.data.pois = update(this.data.pois, {$set: []});
    this.data.pageIndex = update(this.data.pageIndex, {$set: 1});
    this.data.location = update(this.data.location, {$set: {}});
    if (this.data.infs) {
      this.data.infs.resolveLoading();
    }
    this.updateComponent();
  },

  onGetCurrentPosition: function () {
    this.data.geolocation.getCurrentPosition();
  },

  onSetMyPosition: function (result) {
    this.data.myposition = update(this.data.myposition, {latitude: {$set: result.position.lat}});
    this.data.myposition = update(this.data.myposition, {longitude: {$set: result.position.lng}});

    this.data.location = update(this.data.location, {name: {$set: result.formattedAddress}});
    this.data.location = update(this.data.location, {lat: {$set: result.position.lat}});
    this.data.location = update(this.data.location, {lng: {$set: result.position.lng}});
    this.updateComponent();
  },

  onSetlocation: function (obj) {
    this.data.location = update(this.data.location, {name: {$set: obj.name}});
    this.data.location = update(this.data.location, {lat: {$set: obj.lat}});
    this.data.location = update(this.data.location, {lng: {$set: obj.lng}});
    this.updateComponent();
  },

  onSetPosition: function (result) {
    this.data.position = update(this.data.position, {longitude: {$set: result.position.lng}});
    this.data.position = update(this.data.position, {latitude: {$set: result.position.lat}});
    this.updateComponent();
  },

  onSetPlaceSearch: function (placeSearch) {
    this.data.placeSearch = update(this.data.placeSearch, {$set: placeSearch});
    console.log(this.data.position);
    this.updateComponent();
  },

  onSetGeolocation: function (geolocation) {
    this.data.geolocation = update(this.data.geolocation, {$set: geolocation});
    this.updateComponent();
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
